<template>
  <div class="app-container">
    <el-card class="box-card"  style="border-radius: 10px;" v-show="showSearch" >
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="元器件类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option v-for="(item,index) of componentTypeList" :key="index" :value="item.dictValue" :label="item.dictLabel">{{item.dictLabel}}</el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="数据类型" prop="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择类型" clearable size="small">
          <el-option v-for="(item,index) of dataTypeEnum" :key="index" :value="item.value" :label="item.name">{{item.name}}</el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="序号" prop="priority">-->
<!--        <el-input-->
<!--          v-model="queryParams.priority"-->
<!--          placeholder="请输入序号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="值" prop="value">
        <el-input
          v-model="queryParams.value"
          placeholder="请输入值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="备注" prop="remarks">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarks"-->
<!--          placeholder="请输入备注"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>


    <el-card class="box-card margin_t10"  style="border-radius: 10px;" >

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
      <el-upload
        class="upload-demo"
        :on-preview="handlePreview"
        :headers="myHeaders"
        :action="url"
        :on-success="onSuccess"
        multiple
        :limit="1"
       >
        <el-button size="small" type="primary"><i class="el-icon-upload2 el-icon--right"></i> 导入元器件</el-button>
      </el-upload>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="componentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="名称" align="center" prop="name" />
<!--      <el-table-column label="类型" align="center" prop="type" />-->
<!--      <el-table-column label="序号" align="center" prop="priority" />-->
      <el-table-column label="元器件类型" align="center" >
        <template v-if="scope.row.type" slot-scope="scope">
          <span>{{getOneComponentType(scope.row.type)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据类型" align="center" >
        <template v-if="scope.row.dataType" slot-scope="scope">
          <span>{{GlobalVariable.getOneDataType(scope.row.dataType)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="值" align="center" prop="value" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="制造商" align="center" prop="manufacturer" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="功率" align="center" prop="power" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>

    <!-- 添加或修改元器件对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="元器件类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option v-for="(item,index) of componentTypeList" :key="index" :value="item.dictValue" :label="item.dictLabel">{{item.dictLabel}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据类型" prop="type">
          <el-select v-model="form.dataType" placeholder="请选择类型">
            <el-option v-for="(item,index) of dataTypeEnum" :key="index" :value="item.value" :label="item.name">{{item.name}}</el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="序号" prop="priority">-->
<!--          <el-input v-model="form.priority" placeholder="请输入序号" />-->
<!--        </el-form-item>-->
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="form.value" placeholder="请输入值" />
        </el-form-item>
        <el-form-item label="扩展参数" prop="props">
          <el-input v-model="form.props" placeholder="请输入扩展参数（json格式）" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="制造商" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入制造商" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="面积（平方毫米）" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积（平方毫米）" />
        </el-form-item>
        <el-form-item label="功率" prop="power">
          <el-input v-model="form.power" placeholder="请输入功率" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listElement, getElement, delElement, addElement, updateElement } from "@/api/expert/element";
import { getToken } from '@/utils/auth'
import { getDicts } from '@/api/system/dict/data'
export default {
  name: "Element",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 元器件表格数据
      componentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        priority: null,
        code: null,
        value: null,
        remarks: null,
        unit: null,
        manufacturer: null,
        price: null,
        area: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      myHeaders: {},
      url: process.env.VUE_APP_BASE_API + '/circuitDesign/element/importExcel',
      componentTypeList: [],


      // 枚举类型
      dataTypeEnum:this.GlobalVariable.dataTypeEnum,
    };
  },
  created() {
    this.getList();
    this.getDicts();
    this.myHeaders = { Authorization:  'Bearer ' + getToken() }
  },
  methods: {
    /** 查询元器件列表 */
    getList() {
      this.loading = true;
      listElement(this.queryParams).then(response => {
        this.componentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询元器件类型列表 */
    getDicts(){
      getDicts("expert_component_type").then(res => {
        this.componentTypeList = res.data
      })
    },
    getOneComponentType(val){
      for (let i = 0; i < this.componentTypeList.length; i++){
        if (val == this.componentTypeList[i].dictValue){
          return this.componentTypeList[i].dictLabel
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        priority: null,
        code: null,
        value: null,
        remarks: null,
        unit: null,
        props: null,
        manufacturer: null,
        price: null,
        area: null,
        dataType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getDicts()
      this.reset();
      this.open = true;
      this.title = "添加元器件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getDicts()
      this.reset();
      const id = row.id || this.ids
      getElement(id).then(response => {
        this.form = response.data;
        this.form.type = this.form.type.toString();

        if (this.form.props){
          this.form.props = JSON.stringify(this.form.props)
        }

        this.open = true;
        this.title = "修改元器件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.props){
            let jsonObj = JSON.parse(this.form.props);//将JSON字符串转换为JSON对象
            this.form.props = jsonObj
          }

          if (this.form.id != null) {
            updateElement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除元器件编号为"' + ids + '"的数据项？').then(function() {
        return delElement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('edsta/element/export', {
        ...this.queryParams
      }, `edsta_component.xlsx`)
    },
    // 文件上传成功回调
    onSuccess(response, file, fileList) {
      this.$message.success('导入成功')
      this.getList();
      // this.$nextTick(() => {
      //   this.$router.replace({
      //     path: '/redirect' + this.$route.path
      //   })
      // })
    },
  }
};
</script>
